package apptoko.com;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import apptoko.com.adapter.ErrorHandler;
import apptoko.com.background.BackgroundTasks;
import apptoko.com.event.Event.ExchangeTYMFailEvent;
import apptoko.com.event.Event.ExchangeTYMSuccessEvent;
import apptoko.com.event.Event.RequestFailEvent;
import apptoko.com.network.ProgressDialogManager;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;

import apptoko.com.R;
import com.devspark.appmsg.AppMsg;
import com.google.analytics.tracking.android.EasyTracker;

import de.greenrobot.event.EventBus;

public class ExchangeTymActivity extends Activity {

	private String accessToken;
	private BackgroundTasks bgTask;
	private ProgressDialog pDialog;
	private int greenTym;
	private EditText editText;
	private TextView receiveTym;
	private Button btnExchange;
	private Button btnCancel;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_exchange_tym);
		initVariables();
		editText = (EditText) findViewById(R.id.edit_redtym_2);
		receiveTym = (TextView) findViewById(R.id.txt_receive_purple_tym);
		btnExchange = (Button) findViewById(R.id.btn_exchange_2);
		btnCancel = (Button) findViewById(R.id.btn_cancel);
		btnExchange.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!TextUtils.isEmpty(editText.getText().toString())){
					EasyTracker.getTracker().sendEvent(Constants.EVENT_ACTION, "exchange TYM", editText.getText().toString() + " TYM xanh", Long.valueOf(1));
					pDialog = ProgressDialogManager.showProgressDialog(ExchangeTymActivity.this, R.string.loading);
					bgTask.exchangeTYM(accessToken, editText.getText().toString());
				} else {
					AppMsg.makeText(ExchangeTymActivity.this, R.string.input_tym, AppMsg.STYLE_ALERT, null).show();
				}
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		editText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				int exchangeValue;
				if(s.toString().equals("")){
					exchangeValue = 0;
				} else {
					exchangeValue = Integer.parseInt(s.toString());
				}
				if(exchangeValue > greenTym){
					AppMsg.makeText(ExchangeTymActivity.this, R.string.tym_exceed, AppMsg.STYLE_ALERT, null).show();
					editText.setText("0");
				} else {
					receiveTym.setText(String.valueOf(exchangeValue));
				}
			}
		});
	}

	protected void initVariables() {
		// TODO Auto-generated method stub
		accessToken = Utils.getStaticAccessToken(this);
		bgTask = new BackgroundTasks(this);
		greenTym = getIntent().getIntExtra(Constants.GREEN_TYM, greenTym);
		EasyTracker.getInstance().setContext(this);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}
	
	public void onEventMainThread(ExchangeTYMSuccessEvent event){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		finish();
	}
	
	public void onEventMainThread(ExchangeTYMFailEvent event){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		AppMsg.makeText(this, event.getMessage(), AppMsg.STYLE_ALERT, null).show();
	}
	
	public void onEventMainThread(RequestFailEvent ioe){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		ErrorHandler.getInstance().setContext(this).handleError(ioe.getErrorCode());
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		EventBus.getDefault().register(this);
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}
}
